package cst.a931xmc.yuejian20.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import cst.a931xmc.yuejian20.Adapter.CommentAdapter;
import cst.a931xmc.yuejian20.Bean.CommentBean;
import cst.a931xmc.yuejian20.Bean.RegisterBean;
import cst.a931xmc.yuejian20.Bean.ShowBean;
import cst.a931xmc.yuejian20.Bean.UserBean;
import cst.a931xmc.yuejian20.Listener.Listener;
import cst.a931xmc.yuejian20.Model.ApiModel;
import cst.a931xmc.yuejian20.Model.CommentModel;
import cst.a931xmc.yuejian20.Model.ShowModel;
import cst.a931xmc.yuejian20.Model.UserModel;
import cst.a931xmc.yuejian20.R;
import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.internal.Util;

public class ShowActivity extends AppCompatActivity implements View.OnClickListener  {
    private Context context;
    private int show_id;
    private int user_id;
    private String username;
    private String userpic;
    private CircleImageView user_pic;
    private TextView user_name;
    private TextView show_time;
    private TextView show_intro;
    private ImageView show_pic1;
    private ImageView show_pic2;
    private ImageView show_pic3;
    private EditText comment_add;
    private ImageButton btn_back;
    private Button btn_send;
    private ShowModel model;
    private Listener<ShowBean> listener;
    private RecyclerView recyclerView;
    private CommentAdapter adapter;
    private CommentModel model1;
    private Listener<List<CommentBean>> listener1;
    private Listener<RegisterBean> listener2;
    private UserModel model2;
    private Listener<UserBean> listener3;
    SharedPreferences sp;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);
        init();
        initData();
        model = new ShowModel();
        model.getDetail(show_id,listener);
        initViews();
    }

    public void init(){
        user_pic = findViewById(R.id.user_pic);
        user_name = findViewById(R.id.user_name);
        show_time = findViewById(R.id.show_time);
        show_intro = findViewById(R.id.show_intro);
        show_pic1 = findViewById(R.id.show_pic1);
        show_pic2 = findViewById(R.id.show_pic2);
        show_pic3 = findViewById(R.id.show_pic3);
        comment_add = findViewById(R.id.comment_add);
        btn_back = findViewById(R.id.btn_back);
        btn_send = findViewById(R.id.btn_send);
        btn_back.setOnClickListener(this);
        btn_send.setOnClickListener(this);
        Intent intent = getIntent();
        show_id = intent.getExtras().getInt("show_id");
        sp = getSharedPreferences("user_id", Context.MODE_PRIVATE);
        user_id = sp.getInt("user_id",0);
    }

    public void initData(){
        listener = new Listener<ShowBean>() {
            @Override
            public void onResponse(ShowBean l) {
                if(l.getShow_id()!=0){
                    user_name.setText(l.getUser_name());
                    show_time.setText(l.getShow_time());
                    show_intro.setText(l.getShow_intro());
                    ApiModel apiModel = new ApiModel();
                    Picasso.with(context)
                            .load(apiModel.getBaseUrl() + "static/posters/" + l.getUser_pic())
                            .placeholder(R.drawable.nopic)
                            .into(user_pic);
                    Picasso.with(context)
                            .load(apiModel.getBaseUrl() + "static/images/" + l.getShow_pic1())
                            .placeholder(R.drawable.nopic)
                            .into(show_pic1);
                    Picasso.with(context)
                            .load(apiModel.getBaseUrl() + "static/images/" + l.getShow_pic2())
                            .placeholder(R.drawable.nopic)
                            .into(show_pic2);
                    Picasso.with(context)
                            .load(apiModel.getBaseUrl() + "static/images/" + l.getShow_pic3())
                            .placeholder(R.drawable.nopic)
                            .into(show_pic3);
                }
                else {
                    Toast.makeText(ShowActivity.this,"加载失败", Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFail(String message) {
                Toast.makeText(ShowActivity.this,message, Toast.LENGTH_LONG).show();
            }
        };

        listener2 = new Listener<RegisterBean>() {
            @Override
            public void onResponse(RegisterBean l) {
                if(!l.getSuccess().equals("0")){
                    Intent intent = new Intent(ShowActivity.this, ShowActivity.class);
                    intent.putExtra("show_id", show_id);
                    startActivity(intent);
                    finish();
                    Toast.makeText(ShowActivity.this, "评论成功", Toast.LENGTH_LONG).show();
                }
                else {
                    Toast.makeText(ShowActivity.this, "评论失败", Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFail(String message) {
                Toast.makeText(ShowActivity.this, message, Toast.LENGTH_LONG).show();
            }
        };

        getUserInfo();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_send:
                if(TextUtils.isEmpty(comment_add.getText().toString())){
                    Toast.makeText(ShowActivity.this,"请输入评论内容", Toast.LENGTH_LONG).show();
                }
                else {
                    model1.addComment(show_id, user_id, username, userpic,
                            comment_add.getText().toString(), listener2);
                }
                break;
        }
    }

    public void getUserInfo(){
        listener3 = new Listener<UserBean>() {
            @Override
            public void onResponse(UserBean l) {
                username = l.getUser_name();
                userpic = l.getUser_pic();
            }

            @Override
            public void onFail(String message) {
                Toast.makeText(ShowActivity.this, message, Toast.LENGTH_LONG).show();
            }
        };
        model2 = new UserModel();
        model2.getUserDetail(user_id,listener3);
    }

    public void refresh() {
        onCreate(null);
    }

    public void initViews(){
        recyclerView = findViewById(R.id.rv_comment);
        recyclerView.setLayoutManager(new LinearLayoutManager(ShowActivity.this));
        adapter = new CommentAdapter(ShowActivity.this);
        listener1 = new Listener<List<CommentBean>>() {
            @Override
            public void onResponse(List<CommentBean> list) {
                recyclerView.setAdapter(adapter);
                adapter.setList(list);
            }

            @Override
            public void onFail(String message) {

            }
        };
        model1 = new CommentModel();
        model1.getOrderList(show_id,listener1);
    }
}
